package com.platform.controller.platform;

import com.alibaba.fastjson2.JSONObject;
import com.platform.common.annotation.Log;
import com.platform.common.core.domain.AjaxResult;
import com.platform.common.core.page.TableDataInfo;
import com.platform.common.enums.BusinessType;
import com.platform.common.enums.ResultEnum;
import com.platform.common.exception.BusinessException;
import com.platform.common.utils.StringUtils;
import com.platform.system.domain.dto.device.AiUiConfigDto;
import com.platform.system.domain.dto.device.ModelAppInfoDto;
import com.platform.system.domain.vo.device.AiUiConfigVo;
import com.platform.system.domain.vo.device.ModelAppInfoVo;
import com.platform.system.service.ISysAiUiConfigService;
import com.platform.system.service.ISysModelAppInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Iterator;
import java.util.Map;

/**
 * @author Huangkai
 */
@Slf4j
@RestController
@RequestMapping("/model/manage")
public class ModelAppInfoController {

    @Resource
    ISysModelAppInfoService sysModelAppInfoService;

    /**
     * 通过类型查询芯片详情
     */
    @Log(title = "查询大模型配置信息", businessType = BusinessType.OTHER)
    @GetMapping("/selectModelAppInfoList")
    public TableDataInfo selectModelAppInfoList(@Validated ModelAppInfoVo modelAppInfoVo) {
        return sysModelAppInfoService.selectModelAppInfoList(modelAppInfoVo);
    }
    /**
     * 查询详情
     */
    @Log(title = "查询详情", businessType = BusinessType.OTHER)
    @GetMapping("/selectModelAppInfo/{uid}")
    public AjaxResult selectModelAppInfo(@PathVariable(value = "uid") String uid) {
        ModelAppInfoDto modelAppInfoDto = sysModelAppInfoService.selectModelAppInfo(uid);
        if(modelAppInfoDto == null){
            throw new BusinessException(ResultEnum.DATA_NOT_EXIST);
        }
        if(StringUtils.isNotEmpty(modelAppInfoDto.getModelParam())){
            StringBuffer stringBuffer = new StringBuffer();
            JSONObject jsonObject = JSONObject.parseObject(modelAppInfoDto.getModelParam());
            for(Map.Entry<String, Object> entry : jsonObject.entrySet()){
                String key = entry.getKey();
                String value = entry.getValue().toString();
                stringBuffer.append(" " + key + ":" + value + " |");
            }
            if(stringBuffer.length() > 0){
                modelAppInfoDto.setFormatModelParam(stringBuffer.substring(0, stringBuffer.length() - 1 ));
            }
        }
        return AjaxResult.success(modelAppInfoDto);
    }

    /**
     * 新增
     */
    @Log(title = "插入大模型配置", businessType = BusinessType.INSERT)
    @PostMapping("/insertModelAppInfo")
    public AjaxResult insertModelAppInfo(@RequestBody @Validated ModelAppInfoVo modelAppInfoVo) {
        return sysModelAppInfoService.insertModelAppInfo(modelAppInfoVo);
    }

    /**
     * 修改状态
     */
    @Log(title = "修改状态", businessType = BusinessType.UPDATE)
    @PostMapping("/updateModelAppInfoStatus")
    public AjaxResult updateModelAppInfoStatus(@RequestBody @Validated ModelAppInfoVo modelAppInfoVo) {
        return sysModelAppInfoService.updateModelAppInfoStatus(modelAppInfoVo);
    }

    /**
     * 删除
     */
    @Log(title = "删除", businessType = BusinessType.DELETE)
    @PostMapping("/deleteModelAppInfo")
    public AjaxResult deleteModelAppInfo(@RequestBody @Validated ModelAppInfoVo modelAppInfoVo) {
        return sysModelAppInfoService.deleteModelAppInfo(modelAppInfoVo);
    }

    /**
     * 修改
     */
    @Log(title = "修改", businessType = BusinessType.UPDATE)
    @PostMapping("/updateModelAppInfo")
    public AjaxResult updateModelAppInfo(@RequestBody @Validated ModelAppInfoVo modelAppInfoVo) {
        return sysModelAppInfoService.updateModelAppInfo(modelAppInfoVo);
    }


    /**
     * 测试大模型连通性
     */
    @Log(title = "测试大模型连通性", businessType = BusinessType.OTHER)
    @PostMapping("/isModelCollection")
    public AjaxResult isModelCollection(@RequestBody @Validated ModelAppInfoVo modelAppInfoVo) {
        return sysModelAppInfoService.isModelCollection(modelAppInfoVo);
    }

}
